package org.cying.main.controller;

import cn.hutool.http.HttpStatus;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import lombok.extern.slf4j.Slf4j;
import org.cying.main.domain.SampleResult;
import org.cying.main.domain.Student;
import org.cying.main.feign.SignFeignClient;
import org.cying.main.feign.SignFeignStudent;
import org.cying.main.handler.GlobalFlowHandler;
import org.cying.main.handler.SignFlowHandler;
import org.cying.main.util.Method;
import org.cying.main.util.RsaUtil;
import org.cying.main.util.SignUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Sign Server 签到控制类
 * @author Cying
 */
@Slf4j
@RestController
@RefreshScope
@RequestMapping(value = "/sign")
public class SigninController {

    @Resource
    private SignFeignStudent stuSer;

    @Resource
    private SignFeignClient tknSer;

    @Value("${cying.private-key}")
    private String privateKey;

    /**
     * 签到, 检查学号是否在签到队列, 以及 Token 是否正确, 错误时给出相应提示, 成功时从待签到队列移除该学生
     * @param token 令牌
     * @param sid 学号
     * @return 签到结果
     */
    @PostMapping(value = "/sign-auth")
    @SentinelResource(
        value = "sign-auth",
        blockHandlerClass = SignFlowHandler.class,
        blockHandler = "signHandler"
    )
    public SampleResult<Object> signAuth(
        @RequestParam("token") int token,
        @RequestParam("id") String sid
    ) {
        Long SID = RsaUtil.parseSid(sid, privateKey);
        SampleResult<List<Student>> stuRes = stuSer.getBySid(sid, null);
        SampleResult<Boolean> tknRes = tknSer.authToken(token);
        if (stuRes.getStatus() != HttpStatus.HTTP_OK) {
            return new SampleResult<>("/sign/sign-auth", HttpStatus.HTTP_NOT_ACCEPTABLE, null, "签到失败，学号错误或不在执勤名单");
        }
        if (tknRes.getData()) {
            return new SampleResult<>("/sign/sign-auth", HttpStatus.HTTP_NOT_ACCEPTABLE, null, "签到失败，待签到列表没有你的信息");
        }
        SignUtils.getDutyMap().remove(SID);
        return new SampleResult<>("/sign/sign-auth", HttpStatus.HTTP_OK, null, "签到成功");
    }

    /**
     * 刷新值班表接口
     * @return 刷新结果
     */
    @GetMapping(value = "/alter/refresh")
    @SentinelResource(
        value = "alter-refresh",
        blockHandlerClass = GlobalFlowHandler.class,
        blockHandler = "flowHandler"
    )
    public SampleResult<Map<Long, Student>> alterRefresh() {
        SignUtils.getDutyMap().clear();
        if (Method.getTime().getFlag() > 0) {
            String dutyStr = SignUtils.getDuty();
            SampleResult<List<Student>> result = stuSer.getDutyList(dutyStr);
            List<Student> duty = result.getData();
            duty.forEach(student -> {
                SignUtils.getDutyMap().put(student.getSid(), student);
            });
        }
        return new SampleResult<>("/sign/refresh", HttpStatus.HTTP_OK, SignUtils.getDutyMap());
    }

}
